//
//  CXHomeStatusCell.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/22.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit
import SnapKit

class CXHomeStatusCell: UITableViewCell {
    //方便修改下方toolBar的上边界约束
    var toolBarTopCons: Constraint?
    
    /// 再将视图赋值完毕就立刻进行控件内容的赋值 --> 重写didSet
    var statusViewModel: CXStatusViewModel? {
/*
        CXRetweetView:0x7fc07323a500.top == CXOriginalView:0x7fc073234260.bottom>",
        CXToolBarView:0x7fc07323ba60.top == CXRetweetView:0x7fc07323a500.bottom>",
        CXToolBarView:0x7fc07323ba60.top == CXOriginalView:0x7fc073234260.bottom>"
        
*/
        didSet {
            //给各部分赋值内容
            originalView.statusViewModel = statusViewModel
            toolBarView.statusViewModel = statusViewModel
            
            //对于转发部分相关逻辑的处理 比如toolBar的约束更新
            if statusViewModel?.retweetText != nil {
                
                retweetView.hidden = false
                retweetView.statusViewModel = statusViewModel
                
                //涉及到第三者控件，因此要先删除之前的类似约束 再更新为新的约束
                toolBarTopCons?.uninstall()
                //别忘了及时赋值 否则会有报错
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    toolBarTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            } else {
                
                retweetView.hidden = true
                toolBarTopCons?.uninstall()
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    toolBarTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //将各部分创建
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var originalView: CXOriginalView = CXOriginalView()
    private lazy var retweetView: CXRetweetView = CXRetweetView()
    private lazy var toolBarView: CXToolBarView = CXToolBarView()
}

// MARK: - 设置UI
extension CXHomeStatusCell {
    
    private func setupUI() {
        
        backgroundColor = UIColor.lightGrayColor() //设置背景色
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBarView)
        
        //设置约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
            make.trailing.equalTo(contentView)
            //下边界尤其内容决定 在originalView里面设置
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(originalView)
            make.top.equalTo(originalView.snp_bottom)
            make.trailing.equalTo(originalView)
        }
        
        toolBarView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
            self.toolBarTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(35)
            //把设置顶部的约束操作放到模型视图的赋值方法中
        }
        //更新contentView的约束 让cell高度适应其内容
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(toolBarView)
        }
    }
}
